<?php

namespace App\Models\Api;

use Carbon\Carbon;

class ResumeFavorite extends ApiModel
{
    protected $table = 'resume_favorite';

    protected $guarded = [];

    protected $hidden = ['uid', 'created_at', 'updated_at',
        'job_id', 'status', 'resume'];

    public function resume()
    {
        return $this->hasOne(Resume::class, 'id', 'resume_id')
            ->whereBetween('resume_status', [0, 2])//只显示公开的
            ->with('homeaddress_area_name')
            ->select('id', 'name', 'sex', 'birthday', 'education',
                'updatetime', 'intentionjobs', 'avatar', 'homeaddress',
                'chkphoto_open');
    }

    public function company_job()
    {
        return $this->hasOne(CompanyJob::class, 'id', 'job_id')
            ->select('id', 'job_title');
    }

    //更新时间
    public function getTimeAttribute()
    {
        if ($this->updated_at->lt(Carbon::now()->subDays(10))) {
            return $this->updated_at->format('Y-m-d');
        }
        return $this->updated_at->diffForHumans();
    }

    //头像
    public function getAvatarAttribute()
    {
        if (!$this->resume || $this->resume->chkphoto_open == 1) {//隐藏头像
            return '';
        }
        return $this->resume->avatar;
    }

    //简历名
    public function getNameAttribute()
    {
        return $this->resume ? $this->resume->name : '';
    }

    //性别
    public function getSexAttribute()
    {
        return $this->resume ? $this->resume->sex : '';
    }

    //生日
    public function getAgeAttribute()
    {
        return $this->resume ? $this->resume->age : '';
    }

    //期望工作
    public function getIntentionjobsAttribute()
    {
        return $this->resume ? $this->resume->intentionjobs : '';
    }

    //学历
    public function getEducationNameAttribute()
    {
        return $this->resume ? $this->resume->education_name : '';
    }

    public function getHomeaddressNameAttribute()
    {
        return $this->resume ? $this->resume->homeaddress_name : '';
    }
}
